<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
  >
    <div>
      <span>角色名：</span>
      <el-input v-model="searchParams.roleName" />
    </div>
    <div>
      <span>备注：</span>
      <el-input v-model="searchParams.remark" />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="comfire">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRole } from "@/api/xxl-springboot/role.js";

export default {
  props: {
    status: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      searchParams: {
        roleName: "",
        remark: "",
      },
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.status;
      },
      set(val) {
        this.$emit("update:status", val);
      },
    },
  },
  watch: {
    status: {
      handler(newVal) {
        if (newVal) {
          this.resetData();
        }
      },
    },
  },
  methods: {
    resetData() {
      Object.assign(this.$data, this.$options.data());
    },
    async comfire() {
      const { roleName, remark } = this.searchParams;
      const params = {
        role_name: roleName,
        remark,
      };
      const { code } = await addRole(params);
      if (code == 200) {
        this.$emit("update:status", false);
        this.$emit("ok");
      }
    },
    /**
     * 点击右上角的关闭按钮
     * @param {*} done 函数控制是否关闭
     */
    handleClose(done) {
      done();
    },
  },
};
</script>
